package top.sunpxit.generator.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import top.sunpxit.generator.config.DataSourceConfig;
import top.sunpxit.generator.config.SystemConfig;
import top.sunpxit.generator.primary.FieldEntity;
import top.sunpxit.generator.util.BasicUtils;
import top.sunpxit.generator.util.StringUtils;

import java.sql.*;
import java.util.*;

/**
 * @program: set-sail-generator
 * @description: 系统初始化
 * @author: sunpx
 * @create: 2021-03-26 14:11
 **/
public class SystemService {

    private String dataname;

    public void initData(SystemConfig systemConfig) throws SQLException {
        Connection connection = DataSourceConfig.getConnection();
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery("select database() as dataname");
        while (resultSet.next()) {
            dataname = resultSet.getString("dataname");
        }
        String soon="";


        List<String> tableList = systemConfig.getTableList();

        if (BasicUtils.CollectionIsNotEmpty(tableList)) {
            StringBuilder table= new StringBuilder();
            for (String s : tableList) {
                table.append(",'").append(s).append("'");
            }
            soon="and table_name in ("+table+")";
        }



        String sql="select column_name,column_type,column_comment,ordinal_position,column_key,is_nullable,table_name" +
                " from information_schema.columns" +
                " where table_schema ='"+dataname+"'" +
                soon +
                " order by ordinal_position asc";

        ResultSet tableInfo = DataSourceConfig.getConnection().createStatement().executeQuery(sql);
        ResultSetMetaData resultSetMetaData = tableInfo.getMetaData(); //获得结果集结构信息,元数据
        int columnCount = resultSetMetaData.getColumnCount();   //获得列数


        ArrayList<Map<String, Object>> list = new ArrayList<>();
        while (tableInfo.next()) {
            Map<String,Object> rowData = new HashMap<>();
            for (int i = 1; i <= columnCount; i++) {
                        rowData.put(StringUtils.toCamelCase(resultSetMetaData.getColumnName(i)), tableInfo.getObject(i));
            }
            list.add(rowData);
        }

        list.forEach(res->{
            FieldEntity fieldEntity = BeanUtil.toBean(res, FieldEntity.class);
            System.out.println(fieldEntity);
        });

    }

}

/*tableList = new ArrayList<>();

        ResultSet tableNameList = statement.executeQuery("select table_name from information_schema.tables where table_schema='" + dataname + "';");
        ResultSetMetaData md = tableNameList.getMetaData(); //获得结果集结构信息,元数据
        int columnCount = md.getColumnCount();
        while (tableNameList.next()) {
        for (int i = 1; i <= columnCount; i++) {
        tableList.add(tableNameList.getString(i));
        }
        }tableList = new ArrayList<>();

        ResultSet tableNameList = statement.executeQuery("select table_name from information_schema.tables where table_schema='" + dataname + "';");
        ResultSetMetaData md = tableNameList.getMetaData(); //获得结果集结构信息,元数据
        int columnCount = md.getColumnCount();
        while (tableNameList.next()) {
        for (int i = 1; i <= columnCount; i++) {
        tableList.add(tableNameList.getString(i));
        }
        }*/
